﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;

namespace PROSHOP.DAL
{
    public class KhachHangDAL
    {
        ProshopDataContext proshop;

        public KhachHangDAL()
        {
            proshop = new ProshopDataContext();
        }

        public DataSet getThongTinKH(int makh)
        {
            var s = from p in proshop.KhachHangs
                    where p.MaKhachHang == makh
                    select new
                    {
                        p
                    };
            SqlCommand cmd = (SqlCommand)proshop.GetCommand(s);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            da.Fill(ds);
            return ds;
        }

        public bool updateThongTinKH(int makh, string pw, string hoten, bool gioitinh, string email, string diachi, string dienthoai, string cauhoi, string traloi, bool status)
        {
            try
            {
                
                KhachHang kh = proshop.KhachHangs.SingleOrDefault(p => p.MaKhachHang == makh);
                if (kh.Password == pw)
                {
                    kh.Password = pw;
                    kh.HoTen = hoten;
                    kh.GioiTinh = gioitinh;
                    kh.Email = email;
                    kh.DiaChi = diachi;
                    kh.DienThoai = dienthoai;
                    kh.CauHoi = cauhoi;
                    kh.CauTraLoi = traloi;
                    kh.Status = status;
                    proshop.SubmitChanges();
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch (Exception)
            {

                return false;
            }
        }

        public bool themKH(string tendn, string pw, string hoten, bool gioitinh, string email, string diachi, string dienthoai, string cauhoi, string traloi, bool status)
        {
            try
            {
                KhachHang kh = new KhachHang();
                kh.TenDN = tendn;
                kh.Password = pw;
                kh.HoTen = hoten;
                kh.GioiTinh = gioitinh;
                kh.Email = email;
                kh.DiaChi = diachi;
                kh.DienThoai = dienthoai;
                kh.CauHoi = cauhoi;
                kh.CauTraLoi = traloi;
                kh.Status = status;
                proshop.KhachHangs.InsertOnSubmit(kh);
                proshop.SubmitChanges();
                return true;

            }
            catch (Exception)
            {

                return false;
            }
        }

        
    }
}